﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;

namespace SimplePopup
{

    public class SimplePopupProgress
    {

        public Image foregroundImage;
        bool startUpdating = false;
        private float progress;
        private GameObject gameObject;
        private Action onProgressFull;
        private Transform parentTransform;
        private GameObject prefab;
        public SimplePopupProgress() { }
        public SimplePopupProgress(float progress, Transform parentTransform, GameObject prefab)
        {
            this.progress = progress;
            this.parentTransform = parentTransform;
            this.prefab = prefab;
        }

        public void Create()
        {
            gameObject = (GameObject)GameObject.Instantiate(prefab);
            gameObject.transform.SetParent(parentTransform, false);
            gameObject.transform.localScale = new Vector3(1, 1, 1);
            Value = 0f;
            foregroundImage = gameObject.transform.GetChild(1).GetComponent<Image>();
        }

        public void Destroy()
        {
            if (gameObject)
            {
                GameObject.Destroy(gameObject);
            }
        }


        public float Value
        {
            get
            {
                if (foregroundImage != null)
                    return (int)(foregroundImage.fillAmount * 100);
                else
                    return 0;
            }
            set
            {
                if (foregroundImage != null)
                    foregroundImage.fillAmount = value / 100f;
            }
        }

        public void UpdateProgress(float progress)
        {
            this.progress = progress;
            Value = progress;
        }

    }

}
